/**
 * 轮播图
 */
// 调试日志保存 使用方法 logDebug(currentFile,,xxx,xxxx)  xxxx 不限制
const logDebug = require('../logs/log_debug') // 日志工具
const { join } = require('path')  // 路径
const currentFile = join(__filename)    // 当前文件的路径

const { mobuzWx } = require('../config')
const { analysisRequestMessage , compileResponseMessage } =  require('../lib/buffer')
// const { emptyScoreMonthHotData } = require('../lib/common')


const obtainMainBanner = async (ctx) => {
    // await emptyScoreMonthHotData()
    try {
        const _file = __dirname+ '/../proto/BannerInfo.proto';
        // const _reqModal = 'BannerInfo.MainBannerReqMsg';
        const _resModal = 'BannerInfo.MainBannerResMsg';
        // // 解密
        // let messageReq = await analysisRequestMessage( _file, _reqModal, ctx);
        // logDebug(currentFile, messageReq, 'messageReq')
        const mainBannerData = await mobuzWx('web_main_banner').select().first()
        logDebug(currentFile, mainBannerData, 'mainBannerData')
        if (!mainBannerData) {
            ctx.state.data = {
                msg: '没有轮播图数据',
                status: 400
            }
        } else {
            if (!mainBannerData.main_banner) {
                ctx.state.data = {
                    msg: '没有轮播图数据',
                    status: 400
                }
            } else {
                const _resInfo = {
                    status: 200,
                    msg: 'success',
                    data: mainBannerData.main_banner
                }
                let encode = await compileResponseMessage(_file, _resModal, _resInfo)
                ctx.body = encode;
            }
        }
        
    } catch (e) {
        ctx.state = {
            code: -1,
            data: {
                msg: '前方堵塞，服务器繁忙',
                // msg: e,
                status: 500
            }
        }
    }
}

module.exports = {
    obtainMainBanner
}